package mc.contents.web.servlet;

import mc.contents.web.AsyncContextFactory;
import mc.contents.web.SourceConfig;
import mc.contents.web.common.AbstractRequestHttpServlet;
import mc.contents.web.common.CommonContext;
import mc.contents.web.common.SortMap;
import mc.contents.web.listener.RankAppAsyncListener;
import mc.contents.web.listener.ReshelfAppAsyncListener;
import mc.contents.web.request.RankCrawRequest;
import mc.contents.web.request.RankPostSender;
import mc.contents.web.request.ReshelfCrawRequest;
import mc.contents.web.request.ReshelfPostSender;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by LuoHui on 14-11-26.
 */
@WebServlet(name = "reshelfRequest",urlPatterns="/reshelf",asyncSupported=true)
public class ReshelfRequestServlet extends AbstractRequestHttpServlet {
    private static Logger log = LoggerFactory.getLogger( ReshelfRequestServlet.class );
    private static final String name = "reshelf";

    protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        long start = System.currentTimeMillis();
        String nick = request.getParameter( "nick" );
        String keyword = request.getParameter( "keyword" );
        String sortType = request.getParameter( "sortType" );
        String sourceType = request.getParameter( "sourceType" );
        String sessionKey = request.getParameter( "sessionKey" );
        String offset = request.getParameter( "offset" );

        paramsNeed( response,nick,sessionKey,keyword,sourceType,sortType,offset );

        int limit = Integer.parseInt( offset );
        String filter = request.getParameter( "filter" );
        Float price1 = null;
        Float price2 = null;
        if( StringUtils.isNotEmpty( filter )){
            filter = filter.replace( "reserve_price[","" ).replace( "]","" );
            String[] price = filter.split( "," );
            if(StringUtils.isNotEmpty( price[0] )){
                price1 = Float.parseFloat( price[0] );
            }
            if(StringUtils.isNotEmpty( price[1] )){
                price2 = Float.parseFloat( price[1] );
            }
        }
        Long id = atomicId.getAndIncrement();

        AsyncContext asyncContext = request.startAsync(request, response);
        asyncContext.setTimeout( crawConfig.getCrawServletTimeOut() );// 总的超时设置为50s
        asyncContext.addListener( new ReshelfAppAsyncListener( id, start,crawService,name ) );//监听器

        CommonContext context = new CommonContext();
        context.setContext( asyncContext );
        ReshelfCrawRequest crawRequest = new ReshelfCrawRequest(keyword,nick,sessionKey,sourceType,sortType,limit,price1,price2);
        context.setCrawRequest( crawRequest );
        AsyncContextFactory.put( name, id, context );

        asyncContext.start( new ReshelfPostSender(crawRequest,crawConfig, SourceConfig.getSourceConfig( "reshelf" ).getConfigName(),id) );
    }


}
